﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using CommandLib.Base;
using System.Runtime.InteropServices;
using System.Net;

namespace My12306
{
    public partial class FrmMain : Form
    {
        private delegate void dlgWriteInfo(string str);
        public Regex rexStation = new Regex("@([^|]+)\\|([^|]+)\\|([^|]+)\\|(\\d+)", RegexOptions.Compiled);
        public Regex rexEx = new Regex("var message = \"(.+)\"", RegexOptions.Compiled);
        public Regex rexEx2 = new Regex("<span id=\"randErr\"> <img\\s+src='images/er/tools_ico2.gif' width='16' height='16' /><span\\s+style=\"color: red;\">([^<]+)</span> </span>", RegexOptions.Compiled);
        public Regex rexCheckCode = new Regex("<span id=\"randErr\">", RegexOptions.Compiled);
        public Regex rexLogin = new Regex("isLogin\\s*=\\s*true", RegexOptions.Compiled);
        public Regex rexFun = new Regex("javascript:getSelected\\('([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#([^#]+)#(\\d+)'\\)");
        public Regex rexlst = new Regex("(\\d+),<span [^>]+>([^<]+)</span>,((<img[^>]+>)*)([^<]+)<br>([^,]+),((<img[^>]+>)*)([^<]+)<br>([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^\\\\]+)");
        public Regex rexSu = new Regex("id=\"reChooseButton\"", RegexOptions.Compiled);
        public Regex rexMang = new Regex("确认客票的状态后再尝试后续操作。谢谢！", RegexOptions.Compiled);
        public Regex regbed_level_order_num = new Regex("<input type=\"hidden\" name=\"orderRequest.bed_level_order_num\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regcancel_flag = new Regex("<input type=\"hidden\" name=\"orderRequest.cancel_flag\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regend_time = new Regex("<input type=\"hidden\" name=\"orderRequest.end_time\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regfrom_station_name = new Regex("<input type=\"hidden\" name=\"orderRequest.from_station_name\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regfrom_station_telecode = new Regex("<input type=\"hidden\" name=\"orderRequest.from_station_telecode\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regid_mode = new Regex("<input type=\"hidden\" name=\"orderRequest.id_mode\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regseat_type_code = new Regex("<input type=\"hidden\" name=\"orderRequest.seat_type_code\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regstart_time = new Regex("<input type=\"hidden\" name=\"orderRequest.start_time\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regstation_train_code = new Regex("<input type=\"hidden\" name=\"orderRequest.station_train_code\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regticket_type_order_num = new Regex("<input type=\"hidden\" name=\"orderRequest.ticket_type_order_num\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regto_station_name = new Regex("<input type=\"hidden\" name=\"orderRequest.to_station_name\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regto_station_telecode = new Regex("<input type=\"hidden\" name=\"orderRequest.to_station_telecode\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regtrain_date = new Regex("<input type=\"hidden\" name=\"orderRequest.train_date\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regtrain_no = new Regex("<input type=\"hidden\" name=\"orderRequest.train_no\" value=\"([^\"]*)\" id=\"[^\"]+\">", RegexOptions.Compiled);
        public Regex regTOKEN = new Regex("<input type=\"hidden\" name=\"org.apache.struts.taglib.html.TOKEN\" value=\"([^\"]*)\">", RegexOptions.Compiled);
       
        public HttpHelper http = new HttpHelper();
        private FrmLogin login;
        private UcYD frm;
        protected internal DataSetPiaoInfo dataSetPiaoInfo1;

        public bool IsLogin=false;

        public FrmMain()
        {
            InitializeComponent();
            
            this.frm = new UcYD(this);
            this.frm.Dock = DockStyle.Fill;
            this.frm.Location = new Point(3, 3);
            this.frm.Name = "frmYuDing1";
            this.frm.Size = new Size(698, 266);
            this.frm.TabIndex = 0;
            this.tabQiangPiao.Controls.Add(this.frm);
        }

        /// <summary>
        /// 设置 cookies
        /// </summary>
        /// <param name="lpszUrlName"></param>
        /// <param name="lbszCookieName"></param>
        /// <param name="lpszCookieData"></param>
        /// <returns></returns>
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);

        public void ReadData()
        {
            this.frm.dataSetPiaoInfo1.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "ChePiaoData.xml");
        }
       

        private void mbtnLogin_Click(object sender, EventArgs e)
        {
            if (this.login == null || this.login.IsClosed)
            {
                this.login = new FrmLogin(this);
                this.login.Show();
            }
            else
            {
                this.login.Activate();
            }
        }

        public void WriteInfo(string str)
        {
            if (this.txtMsg.InvokeRequired)
            {
                this.txtMsg.Invoke(new FrmMain.dlgWriteInfo(this.WriteInfo), new object[]
				{
					str
				});
            }
            else
            {
                TextBox expr_42 = this.txtMsg;
                expr_42.Text = expr_42.Text + DateTime.Now.ToString("HH:mm:ss: ") + str + "\r\n";
                this.txtMsg.SelectionStart = this.txtMsg.Text.Length - 1;
                this.txtMsg.ScrollToCaret();
                //if (Settings.Default.IsDebug)
                //{
                //    Log.WriteLog4(str, 0);
                //}
            }
        }
        public void WriteHtml(string str)
        {
            //if (Settings.Default.IsDebug)
            //{
            //    Log.WriteLog4(str, 0);
            //}
        }
        public void NavigateMain()
        {
            string text = "https://dynamic.12306.cn/otsweb/main.jsp";
            CookieCollection cookieCollection = this.http.GetCookieCollection(new Uri(text));
            FrmMain.InternetSetCookie("https://dynamic.12306.cn/otsweb/", "JSESSIONID", cookieCollection["JSESSIONID"].Value);
            FrmMain.InternetSetCookie("https://dynamic.12306.cn/", "BIGipServerotsweb", cookieCollection["BIGipServerotsweb"].Value);
            this.Navigate(text);

           
        }
        public void Navigate(string url)
        {
            this.webBrowser.Navigate(url);
            this.webBrowser1.Navigate(url);
            this.tabForm.SelectedTab = this.tabWeb;
        }
        private void GetStationName()
        {
            string hTML = this.http.GetHTML("https://dynamic.12306.cn/otsweb/js/common/station_name.js?version=2.0", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
            this.WriteHtml(hTML);
            foreach (Match match in this.rexStation.Matches(hTML))
            {
                this.frm.dataSetPiaoInfo1.DTCityCode.AddDTCityCodeRow(match.Groups[3].Value, match.Groups[2].Value, match.Groups[1].Value, match.Groups[4].Value);
            }
            this.frm.dataSetPiaoInfo1.FillData();
            this.frm.dataSetPiaoInfo1.WriteXml(AppDomain.CurrentDomain.BaseDirectory + "ChePiaoData.xml");
        }
        public void ChangeState(string state)
        {
            if (base.InvokeRequired)
            {
                base.Invoke(new FrmMain.dlgWriteInfo(this.ChangeState), new object[]
				{
					state
				});
            }
            else
            {
                this.tlbl.Text = state;
            }
        }
    }
}
